#include <fstream>
#include <cstring>
#include "router.h"
#include <cppbase/logging.h>
namespace Frpc
{
	Router::Router()
	{
	}
	
	

	bool Router::add_handler(uint16_t uri, std::function<void(Request *, Reply *,Context*)> f)
	{
		if (handlers.find(uri) != handlers.end())
			return false;
		handlers[uri]=std::move(f);
		return true;
	}

	std::function<void(Request*,Reply*,Context*)> Router::find_handler(uint16_t uri)
	{
		if (handlers.find(uri) == handlers.end())
			return std::function<void(Request*,Reply*,void*)>();
		return handlers[uri];
		
	}

	void Router::handle(Request *req, Reply *reply,Context* ctx)
	{	
		
		auto f=find_handler(req->get_cmd());
		
		if(f){
		
			f(req,reply,ctx);
		}else{
			DLOG("hander NOT found for CMD recieved:0x%X .",req->get_cmd());
			reply->set_bad_output(Status::NotFound);
		}

		return;
	}

}